package jianzhi

func mergeTwoLists1(l1 *ListNode, l2 *ListNode) *ListNode {

	if l1 == nil {
		return l2
	}

	if l2 == nil {
		return l1
	}

	newHead := &ListNode{
		0,
		nil,
	}

	node := newHead

	for l1 != nil && l2 != nil {
		if l1.Val < l2.Val {
			node.Next = l1
			l1 = l1.Next
		} else {
			node.Next = l2
			l2 = l2.Next
		}

		node = node.Next
	}

	if l2 != nil {
		node.Next = l2
	}
	if l1 != nil {
		node.Next = l1
	}

	return newHead.Next
}

func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
	if l1 == nil {
		return l2
	}
	if l2 == nil {
		return l1
	}

	newHead := &ListNode{
		0,
		nil,
	}

	if l1.Val < l2.Val {
		newHead.Val = l1.Val
		newHead.Next = mergeTwoLists(l1.Next, l2)
	} else {
		newHead.Val = l2.Val
		newHead.Next = mergeTwoLists(l1, l2.Next)
	}

	return newHead

}
